#!/usr/bin/env python
#
# Copyright (C) 2009 Remco Brink <remco@rc6.org>
#
# Usage:
#   buzz [user] - shows the last buzz by the specified user
#

"""Read from Buzz"""

import re
from include import feedparser
from include.utils import Module, stripHTML
from include.useragent import geturl
from urlparse import urljoin
from include.BeautifulSoup import BeautifulSoup
import logging as log

class Main(Module):

    enabled = True
    pattern = re.compile(u'^\s*(?:buzz)(?:\s+(\S+))?')
    require_addressing = True
    help = u'buzz [user] - get latest buzz from user'
    baseURL = u'http://buzz.googleapis.com/feeds/'
    feedURL = u'/public/posted'

    def response(self, nick, args, kwargs):
        try:
            try:
                user = args[0]
            except:
                user = None
            if user is None or user == u'':
                return u"%s: No username specified." % nick
            # Build the Buzz URL
            rss_url = rss.join(baseURL, user, feedURL)
            # Fetch the latest updates
            rss = feedparser.parse(rss_url)
            buzz = rss.entries[0].summary
            return u'%s: %s' % (nick, buzz)
        except Exception, error:
            log.warn(u'error in module %s' % self.__module__)
            log.exception(error)
            return u"%s: Couldn't parse the feed for that user - accounts might be protected" % nick


if __name__ == u'__main__':
    from include.utils import test_module
    test_module(Main)
